package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: UsersApiImpl
 * @Author:王晨晗
 * @Description:
 * @Date: 2022/01/05/20:12
 */
@Service
public class UsersApiImpl implements UsersApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @description 保存好友信息
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public String saveUsers(Long userId, Long friendId) {
        //查询数据库判断是否已经是好友关系
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count> 0){
            return null;
        }

        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());

        //注册我与好友的关系
        mongoTemplate.insert(users);

        //注册好友与我的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        mongoTemplate.insert(users);

        return users.getId().toHexString();
    }

    /**
     * @description 删除好友关系
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        Query query1 = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));

        //删除我与好友的关系数据
        long count1 = this.mongoTemplate.remove(query1, Users.class).getDeletedCount();

        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        //删除好友与我的关系数据
        long count2 = this.mongoTemplate.remove(query2, Users.class).getDeletedCount();

        return count1 > 0 && count2 > 0;
    }


    /**
     * @description 查询全部好友列表
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query , Users.class);
    }

    /**
     * @description 根据用户id 分页查询好友列表信息
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1 , pageSize , Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        return mongoTemplate.find(query , Users.class);
    }


}
